﻿/*************************   Auto Generated Repository Code For Facility table     Generated @ 4/16/2013 2:35:18 PM *************************/


using System;
using System.Collections.Generic;
using System.Linq;
using BusinessObjects;

namespace DataLayer
{
    public partial class FacilityRepository
    {
        private PracticeSimpleDataContext dB;


        /// <summary>
        /// Retrieve an Facility by its FacilityID (Primary Key)
        /// </summary>
        /// <param name="FacilityID">The ID of the <see cref="Facility"/> to retrieve</param>
        /// <returns>A <see cref="Facility"/> object</returns>
        public BusinessObjects.Facility GetFacility(int FacilityID)
        {
            Facility retObject;

            using (dB = new PracticeSimpleDataContext())
            {
                retObject = (from x in dB.Facilities where x.FacilityID == FacilityID select x).SingleOrDefault();
            }

            return ConvertToBusinessObject(retObject);
        }

        /// <summary>
        /// Return a list of Facility objects
        /// </summary>
        /// <returns>A List of type <see cref="Facility"/></returns>
        public List<BusinessObjects.Facility> GetFacilitiesList()
        {
            List<BusinessObjects.Facility> retObject = new List<BusinessObjects.Facility>();

            using (dB = new PracticeSimpleDataContext())
            {
                var FacilityList = (from x in dB.Facilities select x).ToList();

                foreach (Facility obj in FacilityList)
                {
                    retObject.Add(ConvertToBusinessObject(obj));
                }

            }
            return retObject;
        }


        /// <summary>
        /// Accepts a Facility Business object, converts it and saves it to the database
        /// </summary>
        /// <returns>Void</returns>
        public void Save(BusinessObjects.Facility businessObject)
        {
            using (dB = new PracticeSimpleDataContext())
            {
                dB.Facilities.InsertOnSubmit(ConvertToDataObject(businessObject));
                dB.SubmitChanges();
            }
        }

        /// <summary>
        /// Accepts the Primary Key of a Facility, finds it in the database and deletes it
        /// </summary>
        /// <param name="FacilityID">The ID of the <see cref="Facility"/> to retrieve</param>
        /// <returns>Void</returns>
        public void Delete(int FacilityID)
        {
            using (dB = new PracticeSimpleDataContext())
            {
                DataLayer.Facility obj = (from x in dB.Facilities where x.FacilityID == FacilityID select x).SingleOrDefault();
                dB.Facilities.DeleteOnSubmit(obj);
                dB.SubmitChanges();
            }
        }

        /// <summary>
        /// Converts a Facility data object to a Facility business object
        /// </summary>
        /// <returns>A business object of type <see cref="Facility"/></returns>
        public BusinessObjects.Facility ConvertToBusinessObject(DataLayer.Facility dataObject)
        {
            BusinessObjects.Facility businessObject = new BusinessObjects.Facility();

            businessObject.FacilityID = dataObject.FacilityID;
            businessObject.ClientID = dataObject.ClientID;
            businessObject.FacilityName = dataObject.FacilityName;


            return businessObject;

        }

        /// <summary>
        /// Converts a Facility business object to a Facility data object
        /// </summary>
        /// <returns>A data object of type <see cref="Facility"/></returns>
        public DataLayer.Facility ConvertToDataObject(BusinessObjects.Facility businessObject)
        {
            DataLayer.Facility dataObject = new DataLayer.Facility();

            dataObject.FacilityID = businessObject.FacilityID;
            dataObject.ClientID = businessObject.ClientID;
            dataObject.FacilityName = businessObject.FacilityName;


            return dataObject;
        }
    }
}


